// StageVu.js for PSPTube
// Made By JK108

function StageVu_CheckURL( url, option )
{
	if (url.match("http://stagevu.com") == "http://stagevu.com"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a StageVu URL
*/
function StageVu_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) { return null; /* failed to get page */ }
	var video_url = contents.match(/\<param name\=\"src\" value\=\"(.*?)\"/);
	var video_url = video_url[1];
	return video_url;
}

function StageVu_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 10;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	if(result.start < perpage){
	var contents = GetContents( "http://stagevu.com/search?for=" + keyword.replace(/ /g, "+") + "&in=Videos" );
	} else{
	var num = 1 + ((result.start - 1) / perpage);
	var contents = GetContents( "http://stagevu.com/search?for=" + keyword.replace(/ /g, "+") + "&in=Videos&page=" + num );
	}
	if(contents == null) { return null; /* failed to get page */ }


	if(contents.match("<div class=\"searcherror\">No videos were found.</div>") == "<div class=\"searcherror\">No videos were found.</div>"){ result.total = 0; return result; }
	var pos = contents.indexOf("<a class=\"oldest current\">");
	if(pos == -1){
		var total = contents.match(/\<a class\=\"oldest\" href\=\"#\" onclick\=\"v\[1\]\[\'page\'\] \= (.*?);/);
		var total = total[1];
		result.total = 10 * parseInt(total);
	} else{
		result.total = 10000;
	}


	while(1){
	pos = contents.indexOf("<div class=\"resultcont\">");

	if(pos != -1){
		contents = contents.substring(pos + 24, contents.length);
		//Get URL
		var url = contents.match(/\<a href\=\"(.*?)\"/);
		var url = url[1];
		//Get Thumbnail
		var thumb_url = contents.match(/\<img src\=\"(.*?)\"/);
		var thumb_url = thumb_url[1];
		//Title
		var title = contents.match(/\<h2\>(.*?)\<\/h2\>/);
		var title = ImproveText(title[1]);
		//Length
		var length = contents.match(/\<span class\=\"bold\"\>Length\: \<\/span\>(.*?),/);
		var length = TimestringToSeconds(length[1]);
		//Description
		var description = contents.match(/\<p\>(.*?)\<\/p\>/);
		var ind1 = contents.indexOf("<p>");
		var ind2 = contents.indexOf("<div class=\"resultcont\">");
		if(description != null && (ind1 < ind2 || result.VideoInfo.length == perpage - 1)){
			var description = splitTo2Lines(ImproveText(description[1]), 60);
		} else{
			var description = contents.match(/\<\/div\>\<strong\>Submitter\: \<\/strong\>(.*?)\<\/div\>/);
			var description = splitTo2Lines(ImproveText(description[1]), 60);
		}
		//Views
		var views = contents.match(/\<span class\=\"bold\"\>Views\: \<\/span\>(.*?),/);
		var views = parseInt(views[1].replace(/,/g, ""));
		
		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.LengthSeconds = length;
		info.Description = description;
		info.ViewCount = views;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.VideoInfo.length == perpage){break;}
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var StageVu = new Object();
StageVu.Name          = "StageVu";		/* Service name */
StageVu.Description   = "StageVu";		/* Service description */
StageVu.SearchDesc    = "StageVu";		/* Service search screen description */
StageVu.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
StageVu.CheckURL      = StageVu_CheckURL;	/* CheckURL function */
StageVu.GetURL        = StageVu_GetURL;		/* GetURL function */
StageVu.Search        = StageVu_Search;		/* Search function */

// add StageVu to site list
SiteList.push( StageVu );
